#!/usr/bin/perl
# IBM_PROLOG_BEGIN_TAG 
# This is an automatically generated prolog. 
#  
#  
#  
# Licensed Materials - Property of IBM 
#  
# Restricted Materials of IBM 
#  
# (C) COPYRIGHT International Business Machines Corp. 2003 
# All Rights Reserved 
#  
# US Government Users Restricted Rights - Use, duplication or 
# disclosure restricted by GSA ADP Schedule Contract with IBM Corp. 
#  
# IBM_PROLOG_END_TAG 
####################################################################
#
#   chadptucode command
#
#   Syntax: chadptucode	[ -f <frame>,<cage>[,<frame>,<cage>...] 
#				-t <timestamp> | -q | --help ]
#
#            --help               - Prints this message
#            -f 		  - <frame>,<cage> is one or more frame,cage 
#					CEC identifiers
#            -t 		  - <timestamp> is the eight-byte timestamp 
#					of an adapter microcode LID file.  A 
#					microcode LID file with the filename 
#					of <timestamp>.ucode must be present 
#					in the directory 
#					/var/hsc/correctiveservice/adapter 
#					on the HMC where the SNM master is 
#					running.
#            -q 		   - Used to display a list of pending adapter 
#					microcode update operations.
#
#
####################################################################
#
#   Initial version - 04/16/03
#
####################################################################

sub exit_routine {
    
     # remove files from /tmp 
     
     exit $ret; 

} # end exit_routine subroutine

sub validate_props {

	$empty = 1;
	
	foreach $ARGV (@ARGV) {
		if ($ARGV =~ /\s*,\S*/ ) { 
			($frame,$cage,$etc) = split(/,/,$ARGV,3);
			$empty = 0;
		}
	}

	if ($frame =~ /\D+/) {
		# non-digit in frame field
		print "non-digit in frame field\n";     #DEBUG
		print $USAGE;      #USAGE
		$ret = 1;
		&exit_routine;
	}
		
	if ($cage =~ /\D+/) {
		# non-digit in cage field
		print "non-digit in cage field\n";     #DEBUG
		print $USAGE;      #USAGE
		$ret = 1;
		&exit_routine;
	}
	
	if ($etc ne "") {
		# invalid parameter
		print "invalid parameter\n";     #DEBUG
		print $USAGE;      #USAGE
		$ret = 1;
		&exit_routine;
	}
		
	if ($empty == 1) {
		# no parameters
		print "no parameters\n";     #DEBUG
		print $USAGE;      #USAGE
		$ret = 1;
		&exit_routine;
	}

}

################################################################################
#
# start main body of code
#
################################################################################
$PRGRM  = "chadptucode";
$BIN    = "/opt/hsc/bin"; 
$ADPTFILE = "/var/hsc/correctiveservice/adapter/ucode.updates";
$PARMS = 0;
$f_USED = 0;
$t_USED = 0;
$q_USED = 0;

$USAGE = "Usage: chadptucode [ -f <frame>,<cage> [<frame>,<cage>...]	\ 
				-t <timestamp> | -q | --help ]\n";


# First check for existence of parameters
if ($#ARGV ne -1) {
	$PARMS = 1;
}

#$i=0;
# Determine if -f was used
foreach $ARGV (@ARGV) {
	#print $ARGV, "\n";
	if ($ARGV =~ /^\s*-f\S*$/ ) { 
		$f_USED = 1;
	}
	#for ($j=1; (!($ARGV[$i+$j] =~ /^\s*-+\S*$/));$j++) {
	#	@fparams = $ARGV[$i+$j];
	#	print $ARGV[$i+$j], "***\n";
	#}
	#$i++;
	
}

$i=0;
# Determine if -t was used
foreach $ARGV (@ARGV) {
	if ($ARGV =~ /^\s*-t\S*$/ ) { 
		$t_USED = 1;
		$timestamp_parm = $ARGV[$i + 1];
		#print $timestamp_parm, "*****\n";
	}
	$i++;
}

# Determine if -q was used
foreach $ARGV (@ARGV) {
	if ($ARGV =~ /^\s*-q\S*$/ ) { 
		$q_USED = 1;
	}
}

# If there are parms, need to validate and parse
if ($PARMS == 1) {
	# Check for --help (usage)
	foreach $ARGV (@ARGV) {
		if ($ARGV =~ /^\s*--help\S*$/ ) { 
			# Help was specified
			print $USAGE;    #USAGE
			if ($#ARGV == 0) {
				$ret = 0;
			}
			else {
				# If help and anything else was specified,
				#      it's an error
				$ret = 1;
			}
			&exit_routine;
		} #end if
	} #end foreach

        if (($f_USED == 1) && ($q_USED == 1)) {
		# Specified both -f and -q
#		print "-f and -q both used\n";     #DEBUG
		print $USAGE;      #USAGE
		$ret = 1;
		&exit_routine;
	}
	
	if (($t_USED == 1) && ($q_USED == 1)) {
		# Specified both -t and -q
#		print "-t and -q both used\n";     #DEBUG
		print $USAGE;      #USAGE
		$ret = 1;
		&exit_routine;
	}
	
	if (($f_USED == 1) && ($t_USED != 1)) {
		# Specified -f, but not -t
#		print "-f used, -t must also be used\n";     #DEBUG
		print $USAGE;      #USAGE
		$ret = 1;
		&exit_routine;
	}
	
	if (($f_USED != 1) && ($t_USED == 1)) {
		# Specified -t, but not -f
#		print "-t used, -f must also be used\n";     #DEBUG
		print $USAGE;      #USAGE
		$ret = 1;
		&exit_routine;
	}
	
	if (($f_USED != 1) && ($t_USED != 1) && ($q_USED != 1)) {
		#No valid flags used
		print "No valid flags used\n";     #DEBUG
		print $USAGE;      #USAGE
		$ret = 1;
		&exit_routine;
	}


	if ($f_USED == 1) {
		&validate_props;
	}

	if (($f_USED == 1) && ($t_USED == 1)) {
		$command = "i_stub_FS adptucode -D ";
		#foreach $fparams (@fparams) {
		foreach $ARGV (@ARGV) {
			if ($ARGV =~ /\s*,\S*/ ) { 
				$command = $command . $ARGV . ",";
			}
		}
		
		#remove extra comma
		chop($command);
		$command = $command . " ";
		
		$command = $command . "-T " . $timestamp_parm;
			
		chop($RC = `$BIN/$command`);
		#print "$BIN/", $command, "\n";
		#print "RC: ", $RC, "\n";
		if ($RC==50)
		{
			print "Cannot connect to FNM Daemon\n";
		}
		if ($RC==51)
		{
			print "One or more frame,cage parameters are invalid\n";
		}
		if ($RC==52)
		{
			print "Device Database not available\n";
		}
		if ($RC==55)
		{
			print "Adapter microcode update file does not exist\n";
		}
		
			
	}
	elsif ($q_USED == 1) {
		#output file contents
		if ((-e $ADPTFILE) && (-r $ADPTFILE)) {
			print "Pending Adapter Microcode Updates:\n";
			print "[frame] [cage] [timestamp]\n";
			system "cat $ADPTFILE";
		}
		else {
			print "There are no pending adapter microcode updates\n";
			#print "File does not exist or is unreadable\n";
		}
	}
	else {
		print $USAGE;      #USAGE
		$ret = 1;
		&exit_routine;
	}

$ret = 0;
$exit_routine;
}
else {
	print $USAGE;      #USAGE
	$ret = 1;
	&exit_routine;
}